package com.baikeyang.app.controller.admin;


import com.baikeyang.common.bean.PageResponseResult;
import com.baikeyang.common.bean.ResponseResult;
import com.baikeyang.common.model.Role;
import com.baikeyang.common.model.RoleResource;
import com.baikeyang.common.util.StringUtils;
import com.baikeyang.common.util.shiro.SecurityUtil;
import com.baikeyang.service.role.IRoleService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

/**
 * Created by lenovo on 2018/7/20.
 */
@RestController(value = "roleController")
@RequestMapping(value = "/admin/role")
public class RoleController {

    @Autowired
    private IRoleService roleService;

    @RequestMapping(value = "index")
    public ModelAndView index () {
        return new ModelAndView("/admin/role/role-index");
    }

    @RequestMapping(value = "list")
    public Object getSysLogs (String page, String recPerPage, String search, String sortBy, String order) {
        //list?page=0&recPerPage=10&sortBy=&order=asc
        Page<Role> pageParam = new Page<Role>();
        Integer size = 10;
        Integer current = 1;
        if(StringUtils.isNotEmpty(recPerPage)){
            try {
                size = Integer.parseInt(recPerPage);
            } catch (Exception e) {}
        }
        if(StringUtils.isNotEmpty(page)) {
            try {
                current = Integer.parseInt(page);
                if(current < 1){current = 1;}
            } catch (Exception e) {}
        }
        pageParam.setSize(size);
        pageParam.setCurrent(current);
        IPage<Role> pageResult = roleService.getRolePage(pageParam);
        PageResponseResult pageResponseResult = new PageResponseResult();
        pageResponseResult.setResult("success");
        pageResponseResult.setData(pageResult.getRecords());
        pageResponseResult.setPager(current, pageResult.getTotal());
        return pageResponseResult;
    }

    @RequestMapping(value = "add")
    public ModelAndView add () {
        return new ModelAndView("/admin/role/role-add");
    }

    @RequestMapping(value = "edit")
    public ModelAndView edit (String id) {
        return new ModelAndView("/admin/role/role-edit")
                .addObject("role", roleService.getById(id));
    }

    @RequestMapping(value = "show")
    public ModelAndView show () {
        return new ModelAndView("/admin/role/role-show");
    }

    @RequestMapping(value = "save")
    public ResponseResult save (Role role) {
        return roleService.saveRole(role);
    }

    @RequestMapping(value = "update")
    public ResponseResult update (Role role) {
        return roleService.update(role);
    }

    @RequestMapping(value = "delete")
    public ResponseResult delete (String ids) {
        return roleService.delete(ids);
    }

    @RequestMapping(value = "conf")
    public ModelAndView config (Integer id) {
        return new ModelAndView("/admin/role/role-config")
                .addObject("role",roleService.getById(id));
    }

    @RequestMapping(value = "saveConf")
    public ResponseResult saveConf (RoleResource roleResource) {
        return roleService.saveRoleResource(roleResource);
    }

    @RequestMapping(value = "getRolesByUId")
    public ResponseResult getRolesByUserId() {
        Long currentUserId = (Long) SecurityUtil.getSubject().getSession().getAttribute("currentUserId");
        //Long currentUserId = (Long) SecurityUtils.getSubject().getSession().getAttribute("currentUserId");
        System.out.println(currentUserId);
        return roleService.getAllRoles();
    }

    @RequestMapping(value = "getResourcesByRoleId")
    public ResponseResult getResourceByRoleId(Integer id) {
        return roleService.getResourceByRoleId(id);
    }

}
